package com.thingcom.delinrobot.main.Launch.NewUser;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.MyLog;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;

import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import static com.thingcom.delinrobot.utils.AppUtils.hideSoftKeyboard;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/12/4
 */
public class RegisterNameFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_register_name)
    SimpleToolbar toolbarRegisterName;
    @BindView(R.id.et_register_name)
    TextInputEditText etRegisterName;
    @BindView(R.id.til_register_name)
    TextInputLayout tilRegisterName;
    @BindView(R.id.et_register_name_two)
    TextInputEditText etRegisterNameTwo;
    @BindView(R.id.til_register_name_two)
    TextInputLayout tilRegisterNameTwo;
    @BindView(R.id.btn_register_continue)
    Button btnRegisterContinue;
    @BindView(R.id.cl_register_name)
    ConstraintLayout clRegisterName;

    private Boolean One = false;
    private Boolean Two = false;

    public static RegisterNameFragment newInstance() {
        Bundle args = new Bundle();
        RegisterNameFragment fragment = new RegisterNameFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_register_name, container, false);
        unbinder = ButterKnife.bind(this, view);
        ColorStateList colorStateList = getResources().getColorStateList(R.color.text_input_edit_selectoe);
        initToolbar(toolbarRegisterName);
        tilRegisterName.setBoxStrokeColorStateList(colorStateList);
        tilRegisterNameTwo.setBoxStrokeColorStateList(colorStateList);
        btnRegisterContinue.setEnabled(false);
        btnRegisterContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));
        clRegisterName.setOnTouchListener((v, event) -> {
            clRegisterName.clearFocus();
            hideSoftKeyboard(getContext(),clRegisterName);
            return false;
        });
        etRegisterName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                MyLog.i("文本", "afterTextChanged: "+s);
                if (s.length()==0){

                    One = false;
                    btnRegisterContinue.setEnabled(false);
                    btnRegisterContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));
                } else {
                    One = true;
                }

                if (One&&Two){
                    btnRegisterContinue.setEnabled(true);
                    btnRegisterContinue.setBackground(getResources().getDrawable(R.drawable.selector_button_main));
                }
            }
        });
        etRegisterNameTwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()==0){
                    Two = false;
                    btnRegisterContinue.setEnabled(false);
                    btnRegisterContinue.setBackgroundColor(getResources().getColor(R.color.grey_button));
                } else {
                    Two = true;
                }

                if (One&&Two){
                    btnRegisterContinue.setEnabled(true);
                    btnRegisterContinue.setBackground(getResources().getDrawable(R.drawable.selector_button_main));
                }
            }
        });
        return view;
    }

    @OnClick(R.id.btn_register_continue)
    public void onViewClicked() {
        hideSoftKeyboard(_mActivity);
        start(RegisterPasswordFragment.newInstance());
        String FirstName = Objects.requireNonNull(etRegisterName.getText()).toString();
        String LastName  = Objects.requireNonNull(etRegisterNameTwo.getText()).toString();
        PreferencesUtil.getInstance(getContext()).putString(Constant.USER_NAME,FirstName+" "+LastName);
    }
}
